import 'dart:convert';

import 'package:dimi_connect/bean/login_entity.dart';
import 'package:dimi_connect/bean/msg_entity.dart';
import 'package:dimi_connect/http/http_request.dart';
import 'package:dio/dio.dart';

class Api {
  static void sendSms(String phone, {Function success, Function error}) {
    HttpUtil.postForm("smsSend/",
        data: FormData.fromMap({"phone": phone, "aa": "aa"}),
        success: (String res) {
      MsgEntity entity = MsgEntity().fromJson(jsonDecode(res));
      if (entity.code == 200) {
        success("验证码发送成功");
      } else {
        error(entity.msg);
      }
    }, error: error);
  }

  static void login(String phone, String code,
      {Function success, Function error}) {
    HttpUtil.postForm("api_login/",
        data: FormData.fromMap({"phone": phone, "code": code}),
        success: (String res) {
      LoginEntity entity = LoginEntity().fromJson(jsonDecode(res));
      if (entity.code == 200) {
        success(entity.data);
      } else {
        error(entity.msg);
      }
    }, error: error);
  }

  static void wifiLogin(String ip, Map<String, dynamic> map,
      {Function success, Function error}) {
    LoginData user = LoginData().fromJson(map);

    HttpUtil.get("api_wifi_login/", data: {
      "Thephone": user.phone,
      "name": user.name,
      "uid": user.id,
      "ip": ip,
      "fac": "pc"
    }, success: (String res) {
      MsgEntity entity = MsgEntity().fromJson(jsonDecode(res));
      if (entity.code == 200) {
        success();
      } else {
        error(entity.msg);
      }
    }, error: error);
  }
}
